using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Biology_HermitCrab_Ctrl : Unit_Biology_Ctrl
{
    public Animator animator;
    public GameObject default_Body;
    public GameObject defence_Body;
    int targetIndex = 0;
    public Transform point_0;
    public Transform point_1;
    public int direction = 0;
    public float speed = 0.32f;
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        if (point_0 != null && point_1 != null)
            Gizmos.DrawLine(new Vector3(point_0.transform.position.x,this.transform.position.y), new Vector3(point_1.transform.position.x, this.transform.position.y));
    }

    public override void Start()
    {
        base.Start();
        ai_Ctrl = new Ai_Ctrl();
        float aiTime = 0;
        ai_Ctrl.AddState(new Ai_State("Patrol", () =>
        {
            if (point_0 == null || point_1 == null)
            {
                ai_Ctrl.SetState("Defence");
                return;
            }
            animator.Play("Walk");
            default_Body.gameObject.SetActive(true);
            defence_Body.gameObject.SetActive(false);
        }, () =>
        {
            if (point_0 == null || point_1 == null)
            {
                ai_Ctrl.SetState("Defence");
                return;
            }
            Vector3 targetPos = targetIndex % 2 == 0 ? point_0.transform.position : point_1.transform.position;
            float direction = (targetPos.x - this.transform.position.x);
            if (Mathf.Abs(this.transform.position.x - targetPos.x) <= 0.04f)
            {
                targetIndex++;
            }
            this.transform.transform.position += new Vector3(direction, 0).normalized * Time.deltaTime * speed;
            this.transform.transform.localScale = new Vector3(direction > 0 ? 1 : -1, 1, 1);
        }));
        ai_Ctrl.AddState(new Ai_State("Defence", () =>
        {
            default_Body.gameObject.SetActive(false);
            defence_Body.gameObject.SetActive(true);
            aiTime = 4;
        }, () =>
        {
            aiTime -= Time.deltaTime;
            if (aiTime <= 0)
            {
                ai_Ctrl.SetState("Patrol");
            }
        }));
        ai_Ctrl.SetState("Patrol");
    }


    public void ReceiveLightEvent(Vector3 sourcePos, Vector3 sourceDirection)
    {
        ai_Ctrl.SetState("Defence");
    }
}
